package com.master.demo.controller.app;

import cn.hutool.core.util.RandomUtil;

import com.master.common.api.Query;
import com.master.common.api.ResponseInfo;
import com.master.common.enums.IntEnum;
import com.master.common.enums.ResponseEnum;
import com.master.common.enums.StatusEnum;
import com.master.common.validator.ValidatorUtil;
import com.master.demo.common.constant.RedisConstants;
import com.master.demo.common.constant.SmsConstant;
import com.master.demo.common.util.AliyunSMSUtil;
import com.master.demo.common.util.RedisUtils;
import com.master.demo.framework.base.BaseController;
import com.master.demo.sys.pojo.vo.AdvertisingVO;
import com.master.demo.sys.pojo.vo.NoticeVO;
import com.master.demo.sys.service.IAdvertisingService;
import com.master.demo.sys.service.INoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信小程序接口(无需鉴权接口)
 *
 * @author: Yang
 * @date: 2022/8/16
 * @version: 1.0.0
 * Copyright Ⓒ 2023 文淼 Computer Corporation Limited All rights reserved.
 */
@Slf4j
@RestController(value = "appApiController")
@RequestMapping("/app/api")
public class AppApiController extends BaseController {
    @Autowired
    private IAdvertisingService advertisingService;
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取首页广告位置信息
     *
     * @author: Yang
     * @date: 2023/06/26
     * @version: 1.0.0
     */
    @GetMapping("/advertising")
    public ResponseInfo<List<AdvertisingVO>> listAdvertising(@RequestParam Map map) {
        map.put("status", StatusEnum.ENABLED.getValue());
        return advertisingService.listAdvertising(new Query(map));
    }

    /**
     * 获取首页配置
     *
     * @author: Yang
     * @date: 2023/1/9
     * @version: 1.0.0
     */
    @GetMapping("/home/config")
    public ResponseInfo getHomeConfig() {
        return ResponseInfo.success();
    }

    /**
     * 发送短信
     *
     * @author: Yang
     * @date: 2023/1/9
     * @version: 1.0.0
     */
    @PostMapping("/sms/send")
    public ResponseInfo sendSmsCode(@RequestBody Query query) {
        query = new Query(query);
        String tel = query.getTel();
        if (IntEnum.ELEVEN.getValue() != tel.length()) {
            return ResponseInfo.businessFail("请输入正确手机号码");
        }
        String code = RandomUtil.randomNumbers(6);
        Map<String, Object> map = new HashMap<>(16);
        map.put("code", code);
        String key = RedisConstants.TEL_CODE + tel;
        if (ValidatorUtil.isNotNull(redisUtils.get(key))) {
            return ResponseInfo.customizeError(ResponseEnum.REPEAT_REQUEST_SMS);
        }
        AliyunSMSUtil.sendSms(tel, SmsConstant.AUTHCODE_TEMPLATE, map);
        redisUtils.set(key, code, RedisConstants.FIVE_MINUTES);
        return ResponseInfo.success();
    }

    /**
     * 获取系统公告/常见问题
     *
     * @author: Yang
     * @date: 2023/1/9
     * @version: 1.0.0
     */
    @GetMapping("/notice")
    public ResponseInfo<List<NoticeVO>> listNotice(@RequestParam(required = false) Integer type, @RequestParam(required = false) Integer subType) {
        return noticeService.listNotice(type, subType);
    }

}
